package data;

import data.ConnectionManager;
import utils.UIDGenerator;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import domain.Queja;
import domain.Usuario;

public class JDBCQuejaDAO implements IQuejaDAO {
	
	private Connection conn;
	
	public JDBCQuejaDAO(){
		conn = ConnectionManager.getInstance().checkOut();
		System.out.println("SE HA CREADO LA CONEXION");
	}

	public void insert(Queja q) {
		// TODO Auto-generated method stub
		String oido = UIDGenerator.getInstance().getKey();
        PreparedStatement stmt = null;
        String sql = "INSERT INTO quejas ( OIDQueja, usuario, fecha, distrito, tema, ubicacion, mensaje, estado, titulo) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ";
        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, oido);
            stmt.setString(2, q.getUsuario());
            stmt.setString(3, "");
            stmt.setString(4, q.getDistrito());
            stmt.setString(5, q.getTema());
            stmt.setString(6, q.getUbicacion());
            stmt.setString(7, q.getMensaje());
            stmt.setString(8, q.getEstado());
            stmt.setString(9, q.getTitulo());
    

            stmt.executeUpdate();


        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
        	System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }


	}

	@Override
	public Queja select(String idQueja) {
		// TODO Auto-generated method stub
		PreparedStatement stmt = null;
        ResultSet result = null;
        Queja q=null;
        
        try {

            String sql = "SELECT * FROM   quejas WHERE OIDQueja = ?";
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            stmt.setString(1, idQueja);
            stmt.executeQuery();
            result = stmt.executeQuery();

            result.next();
            q = new Queja();
            q.setUsuario(result.getString("usuario"));
            q.setDistrito(result.getString("distrito"));
            q.setTema(result.getString("tema"));
            q.setUbicacion(result.getString("ubicacion"));
            q.setMensaje(result.getString("mensaje"));
			q.setEstado(result.getString("estado"));
			q.setTitulo(result.getString("titulo"));
           
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally { 
            ConnectionManager.getInstance().checkIn(conn);
            System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return q;

	}

	@Override
	public List<Queja> selectAllQuejas() {
		// TODO Auto-generated method stub
		PreparedStatement stmt=null;
		ResultSet result=null;
		List<Queja> searchResults=new LinkedList<Queja>();
		
		String sql="SELECT * FROM quejas";
		
		try{
			stmt=conn.prepareStatement(sql);
			stmt.executeQuery();
			result=stmt.executeQuery();
			
			while(result.next()){
				
				Queja q = new Queja();
				q.setIdQueja(result.getString("OIDQueja"));
				q.setUsuario(result.getString("usuario"));
	            q.setDistrito(result.getString("distrito"));
	            q.setTema(result.getString("tema"));
	            q.setUbicacion(result.getString("ubicacion"));
	            q.setMensaje(result.getString("mensaje"));
				q.setEstado(result.getString("estado"));
				q.setTitulo(result.getString("titulo"));
				searchResults.add(q);
			}
				
			} catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
		return searchResults;
	}

}
